package cn.wei.reallyenjoy.user.controller;


import cn.wei.reallyenjoy.common.entity.User;
import cn.wei.reallyenjoy.common.result.Result;
import cn.wei.reallyenjoy.user.pojo.vo.LoginVO;
import cn.wei.reallyenjoy.user.pojo.vo.RegisterVO;
import cn.wei.reallyenjoy.user.pojo.vo.UserInfoVO;
import cn.wei.reallyenjoy.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author Wei
 * @since 2021-11-28
 */
@RestController
@Api(tags = "用户管理")
@RefreshScope
@RequestMapping("/user")
public class UserController {

    @Value("${config.info}")
    private String info;

    @Resource
    private UserService userService;

    @ApiOperation("登录")
    @PostMapping("/login")
    public Result login(LoginVO loginVO){
        System.out.println(loginVO);
        UserInfoVO userInfo = userService.login(loginVO);
        Assert.notNull(userInfo, "账号或密码错误");
        return Result.succ("登录成功",userInfo);
    }
    @ApiOperation("用户列表")
    @GetMapping("/list")
    public Result listAll(){
        List<User> list = userService.list(null);
        return Result.succ("ok", list);
    }

    @ApiOperation("注册")
    @PostMapping("/register")
    public Result register(RegisterVO registerVO){
        Boolean res = userService.isRegister(registerVO.getMobile());
        if (res){
            return Result.fail("该用户已注册");
        }
        userService.register(registerVO);
        return Result.succ("注册成功",null);
    }

    @ApiOperation("测试")
    @PostMapping("/test")
    public Result test(){
        return Result.succ(null);
    }

    @ApiOperation("User服务《分布式配置中心》演示：版本信息更换")
    @PostMapping("/testConfigCenter")
    public Result testConfigCenter(){
        return Result.succ(null,info);
    }
}

